﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Bussines;

namespace Bussines
{
    public class clORSZAG
    {
        public int fORS_ID;
        public string fORS_MEGNEVEZES;

        public clORSZAG(int iORS_ID, string iORS_MEGNEVEZES)
        {
            fORS_ID = iORS_ID;
            fORS_MEGNEVEZES = iORS_MEGNEVEZES;
        }
    }

    public class clORSZAG_LIST
    {
        private SqlConnection sc;
        TBLObj pBLObj = new TBLObj();

        public List<clORSZAG> lORSZAG = new List<clORSZAG>() ;

        public clORSZAG_LIST(TBLObj iBLObj)
        {
            pBLObj = iBLObj;            
            sc = iBLObj.sqlConnection;

            SqlCommand cmd = new SqlCommand();

            cmd.Connection = sc;
            cmd.CommandText = "SELECT ORS_ID, ORS_MEGNEVEZES " +
                "FROM ORSZAG";

            cmd.CommandType = CommandType.Text;

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                clORSZAG o = new clORSZAG((int)rdr["ORS_ID"], rdr["ORS_MEGNEVEZES"].ToString());
                lORSZAG.Add(o);
            }
            rdr.Close();
        }
    }

    
    public class clPARTNER_CIM
    {        
        public int fCMB_ID;
        public int fCMB_PRT_ID;
        public clORSZAG fCMB_ORS_ID;
        public int fCMB_IRS_ID;
        public string fCMB_VAROS;
        public string fCMB_UTCA;
        public string fCMB_MEGJEGYZES;
        public int fCMB_TIPUS;
        public string fIRS_SZAM;

        public clPARTNER_CIM(int iCMB_ID, int iCMB_PRT_ID, clORSZAG iCMB_ORS_ID, int iCMB_IRS_ID, string iCMB_VAROS, string iCMB_UTCA, string iCMB_MEGJEGYZES, int iCMB_TIPUS, string iIRS_SZAM)
        {
            fCMB_ID = iCMB_ID;
            fCMB_PRT_ID = iCMB_PRT_ID;
            fCMB_ORS_ID = iCMB_ORS_ID;
            fCMB_IRS_ID = iCMB_IRS_ID;
            fCMB_VAROS = iCMB_VAROS;
            fCMB_UTCA = iCMB_UTCA;
            fCMB_MEGJEGYZES = iCMB_MEGJEGYZES;
            fCMB_TIPUS = iCMB_TIPUS;
            fIRS_SZAM = iIRS_SZAM;
        }
    }
    public class clPARTNER_CIM_LIST
    {
        private SqlConnection sc;
        TBLObj pBLObj = new TBLObj();

        public List<clPARTNER_CIM> lPARTNER_CIM = new List<clPARTNER_CIM>();

        public clPARTNER_CIM_LIST(TBLObj iBLObj)
        {
            pBLObj = iBLObj;
            
            sc = iBLObj.sqlConnection;
            
        }
        public clPARTNER_CIM_LIST(TBLObj iBLObj, int iPRT_ID)
        {
            TBLObj pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;

            SqlCommand cmd = new SqlCommand();

            //EMAIL LIST
            cmd.Connection = sc;
            cmd.CommandText = "SELECT CMB_ID, CMB_PRT_ID, CMB_ORS_ID, IRS_SZAM, CMB_VAROS, CMB_UTCA, CMB_TIPUS, CMB_MEGJEGYZES , ORS_MEGNEVEZES , CMB_IRS_ID " +
                "FROM PARTNER_CIM " +
                "INNER JOIN ORSZAG ON ORS_ID = CMB_ORS_ID " + 
                "INNER JOIN IRSZ ON IRS_ID = CMB_IRS_ID "+
                "WHERE CMB_PRT_ID = " + iPRT_ID.ToString() + " ";

            cmd.CommandType = CommandType.Text;

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                clORSZAG cOrsz = new clORSZAG((int)rdr["CMB_ORS_ID"], rdr["ORS_MEGNEVEZES"].ToString());
                clPARTNER_CIM pe = new clPARTNER_CIM((int)rdr["CMB_ID"], (int)rdr["CMB_PRT_ID"], cOrsz, (int)rdr["CMB_IRS_ID"], rdr["CMB_VAROS"].ToString(), rdr["CMB_UTCA"].ToString(), rdr["CMB_MEGJEGYZES"].ToString(), (int)rdr["CMB_TIPUS"], rdr["IRS_SZAM"].ToString());
                lPARTNER_CIM.Add(pe);
            }
            rdr.Close();
        }

        public bool SavePartnerCim(ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();

            cmd.Connection = sc;
            cmd_insert.Connection = sc;

            cmd.CommandType = CommandType.Text;
            cmd_insert.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE dbo.PARTNER_CIM SET " +
                " CMB_ORS_ID = @CMB_ORS_ID , " +
                " CMB_IRS_ID = @CMB_IRS_ID , " +
                " CMB_VAROS = @CMB_VAROS , " +
                " CMB_UTCA = @CMB_UTCA , " +
                " CMB_MEGJEGYZES = @CMB_MEGJEGYZES , " +
                " CMB_TIPUS = @CMB_TIPUS " +
                "WHERE " +
                " CMB_ID = @CMB_ID ";

            cmd.Parameters.Add(new SqlParameter("CMB_ORS_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("CMB_IRS_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("CMB_VAROS", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("CMB_UTCA", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("CMB_MEGJEGYZES", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("CMB_TIPUS", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("CMB_ID", SqlDbType.Int));

            cmd_insert.CommandText = "INSERT INTO PARTNER_CIM " +
                "( CMB_PRT_ID, CMB_ORS_ID, CMB_IRS_ID, CMB_VAROS, CMB_UTCA, CMB_MEGJEGYZES, CMB_TIPUS) " +
                "VALUES " +
                "( @CMB_PRT_ID, @CMB_ORS_ID, @CMB_IRS_ID, @CMB_VAROS, @CMB_UTCA, @CMB_MEGJEGYZES, @CMB_TIPUS) ";

            cmd_insert.Parameters.Add(new SqlParameter("CMB_ORS_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_IRS_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_VAROS", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_UTCA", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_MEGJEGYZES", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_TIPUS", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("CMB_PRT_ID", SqlDbType.Int));

            foreach (clPARTNER_CIM akt in lPARTNER_CIM)
            {
                if (akt.fCMB_IRS_ID != -1)
                {
                    if (akt.fCMB_ID != -1)
                    {
                        cmd.Parameters["CMB_ORS_ID"].Value = akt.fCMB_ORS_ID.fORS_ID;
                        cmd.Parameters["CMB_IRS_ID"].Value = akt.fCMB_IRS_ID;
                        cmd.Parameters["CMB_VAROS"].Value = akt.fCMB_VAROS;
                        cmd.Parameters["CMB_UTCA"].Value = akt.fCMB_UTCA;
                        cmd.Parameters["CMB_MEGJEGYZES"].Value = akt.fCMB_MEGJEGYZES;
                        cmd.Parameters["CMB_TIPUS"].Value = akt.fCMB_TIPUS;
                        cmd.Parameters["CMB_ID"].Value = akt.fCMB_ID;
                        try
                        {
                            cmd.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {

                            string s = "Hiba a partner címének mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                    else
                    {
                        cmd_insert.Parameters["CMB_ORS_ID"].Value = akt.fCMB_ORS_ID.fORS_ID;
                        cmd_insert.Parameters["CMB_IRS_ID"].Value = akt.fCMB_IRS_ID;
                        cmd_insert.Parameters["CMB_VAROS"].Value = akt.fCMB_VAROS;
                        cmd_insert.Parameters["CMB_UTCA"].Value = akt.fCMB_UTCA;
                        cmd_insert.Parameters["CMB_MEGJEGYZES"].Value = akt.fCMB_MEGJEGYZES;
                        cmd_insert.Parameters["CMB_TIPUS"].Value = akt.fCMB_TIPUS;
                        cmd_insert.Parameters["CMB_PRT_ID"].Value = akt.fCMB_PRT_ID;

                        try
                        {
                            cmd_insert.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {
                            string s = "Hiba a partner címének mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                }
                
            }


            return true;
        }

        public void FillPRT_ID(int iPRT_ID)
        {
            foreach (clPARTNER_CIM akt in lPARTNER_CIM)
            {
                akt.fCMB_PRT_ID = iPRT_ID;
            }
        }
    }

    public class clPARTNER_MAIL
    {
        public int fEML_ID; 
        public int fEML_PRT_ID;
        public string fEML_MAILCIM;
        public string fEML_MEGJEGYZES;

        public clPARTNER_MAIL(int iEML_ID, int iEML_PRT_ID, string iEML_MAILCIM, string iEML_MEGJEGYZES)
        {
            fEML_ID = iEML_ID;
            fEML_PRT_ID = iEML_PRT_ID;
            fEML_MAILCIM = iEML_MAILCIM;
            fEML_MEGJEGYZES = iEML_MEGJEGYZES;
        }
    }
    public class clPARTNER_MAIL_LIST
    {
        private SqlConnection sc;
        TBLObj pBLObj = new TBLObj();

        public List<clPARTNER_MAIL> lPARNTER_MAIL = new List<clPARTNER_MAIL>();

        public clPARTNER_MAIL_LIST(TBLObj iBLObj)
        {
            pBLObj = iBLObj;
            
            sc = iBLObj.sqlConnection;
        }
        public clPARTNER_MAIL_LIST(TBLObj iBLObj , int iPRT_ID)
        {
            pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;

            SqlCommand cmd = new SqlCommand();

            //EMAIL LIST
            cmd.Connection = sc;
            cmd.CommandText = "SELECT EML_ID, EML_PRT_ID, EML_MAILCIM , EML_MEGJEGYZES " +
                "FROM PARTNER_MAIL " +
                "WHERE EML_PRT_ID = " + iPRT_ID.ToString() + " ";

            cmd.CommandType = CommandType.Text;

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                clPARTNER_MAIL pe = new clPARTNER_MAIL((int)rdr["EML_ID"], (int)rdr["EML_PRT_ID"], rdr["EML_MAILCIM"].ToString(), rdr["EML_MEGJEGYZES"].ToString());
                lPARNTER_MAIL.Add(pe);
            }
            rdr.Close();
        }
        public bool SavePartnerEmail(ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();

            cmd.Connection = sc;
            cmd_insert.Connection = sc;

            cmd.CommandType = CommandType.Text;
            cmd_insert.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE PARTNER_MAIL SET " +
                " EML_MAILCIM = @EML_MAILCIM, " +
                " EML_MEGJEGYZES = @EML_MEGJEGYZES " +
                "WHERE EML_ID = @EML_ID ";

            cmd.Parameters.Add(new SqlParameter("EML_MAILCIM", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("EML_MEGJEGYZES", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("EML_ID", SqlDbType.Int));

            cmd_insert.CommandText = "INSERT INTO PARTNER_MAIL " +
                "( EML_PRT_ID, EML_MAILCIM, EML_MEGJEGYZES ) " +
                "VALUES " +
                "( @EML_PRT_ID,@EML_MAILCIM,@EML_MEGJEGYZES) ";

            cmd_insert.Parameters.Add(new SqlParameter("EML_MAILCIM", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("EML_MEGJEGYZES", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("EML_PRT_ID", SqlDbType.Int));

            foreach (clPARTNER_MAIL akt in lPARNTER_MAIL)
            {
                if (akt.fEML_MAILCIM != "")
                {
                    if (akt.fEML_ID != -1)
                    {
                        cmd.Parameters["EML_ID"].Value = akt.fEML_ID;
                        cmd.Parameters["EML_MAILCIM"].Value = akt.fEML_MAILCIM;
                        cmd.Parameters["EML_MEGJEGYZES"].Value = akt.fEML_MEGJEGYZES;
                        try
                        {
                            cmd.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {
                            string s = "Hiba a partner email címének mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                    else
                    {
                        cmd_insert.Parameters["EML_PRT_ID"].Value = akt.fEML_PRT_ID;
                        cmd_insert.Parameters["EML_MAILCIM"].Value = akt.fEML_MAILCIM;
                        cmd_insert.Parameters["EML_MEGJEGYZES"].Value = akt.fEML_MEGJEGYZES;
                        try
                        {
                            cmd_insert.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {
                            string s = "Hiba a partner email címének mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                }
                
            }


            return true;
        }

        public void FillPRT_ID(int iPRT_ID)
        {
            foreach (clPARTNER_MAIL akt in lPARNTER_MAIL )
            {
                akt.fEML_PRT_ID = iPRT_ID;
            }
        }
    }

    public class clPARTNER_TELEFON
    {
        public int fTLF_ID; 
        public int fTLF_TIPUS;
        public int fTLF_PRT_ID;
        public string fTLF_SZAM;
        public string fTLF_MEGJEGYZES;

        public clPARTNER_TELEFON(int iTLF_ID, int iTLF_TIPUS, int iTLF_PRT_ID, string iTLF_SZAM, string iTLF_MEGJEGYZES)
        {
            fTLF_ID = iTLF_ID;
            fTLF_TIPUS = iTLF_TIPUS;
            fTLF_PRT_ID = iTLF_PRT_ID;
            fTLF_SZAM = iTLF_SZAM;
            fTLF_MEGJEGYZES = iTLF_MEGJEGYZES;
        }

    }
    public class clPARTNER_TELEFON_LIST
    {
        private SqlConnection sc;
        TBLObj pBLObj = new TBLObj();

        public List<clPARTNER_TELEFON> lPARNTER_TELEFON = new List<clPARTNER_TELEFON>();

        public clPARTNER_TELEFON_LIST(TBLObj iBLObj)
        {
            pBLObj = iBLObj;
            
            sc = iBLObj.sqlConnection;
        }
        public clPARTNER_TELEFON_LIST(TBLObj iBLObj , int iPRT_ID)
        {
            pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;

            SqlCommand cmd = new SqlCommand();

            //TELEFON LIST
            cmd.Connection = sc;
            cmd.CommandText = "SELECT TLF_ID, TLF_TIPUS, TLF_PRT_ID, TLF_SZAM , TLF_MEGJEGYZES " +
                "FROM PARTNER_TELEFON " +
                "WHERE TLF_PRT_ID = " + iPRT_ID.ToString() + " ";

            cmd.CommandType = CommandType.Text;

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                clPARTNER_TELEFON pt = new clPARTNER_TELEFON((int)rdr["TLF_ID"], (int)rdr["TLF_TIPUS"], (int)rdr["TLF_PRT_ID"], rdr["TLF_SZAM"].ToString(), rdr["TLF_MEGJEGYZES"].ToString());
                lPARNTER_TELEFON.Add(pt);
            }
            rdr.Close();
        }

        public bool SavePartnerTelefon(ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();

            cmd.Connection = sc;
            cmd_insert.Connection = sc;

            cmd.CommandType = CommandType.Text;
            cmd_insert.CommandType = CommandType.Text;

            cmd.CommandText = "UPDATE PARTNER_TELEFON SET " +
                " TLF_TIPUS = @TLF_TIPUS , " +
                " TLF_SZAM = @TLF_SZAM , " +
                " TLF_MEGJEGYZES = @TLF_MEGJEGYZES " +
                "WHERE TLF_ID = @TLF_ID ";

            cmd.Parameters.Add(new SqlParameter("TLF_SZAM", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("TLF_MEGJEGYZES", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("TLF_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("TLF_TIPUS", SqlDbType.Int));

            cmd_insert.CommandText = "INSERT INTO PARTNER_TELEFON " +
                "( TLF_TIPUS, TLF_PRT_ID, TLF_SZAM, TLF_MEGJEGYZES ) " +
                "VALUES " +
                "( @TLF_TIPUS, @TLF_PRT_ID, @TLF_SZAM, @TLF_MEGJEGYZES ) ";

            cmd_insert.Parameters.Add(new SqlParameter("TLF_SZAM", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("TLF_MEGJEGYZES", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("TLF_PRT_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("TLF_TIPUS", SqlDbType.Int));

            foreach (clPARTNER_TELEFON akt in lPARNTER_TELEFON)
            {
                if (akt.fTLF_SZAM != "")
                {
                    if (akt.fTLF_ID != -1)
                    {
                        cmd.Parameters["TLF_SZAM"].Value = akt.fTLF_SZAM;
                        cmd.Parameters["TLF_MEGJEGYZES"].Value = akt.fTLF_MEGJEGYZES;
                        cmd.Parameters["TLF_ID"].Value = akt.fTLF_ID;
                        cmd.Parameters["TLF_TIPUS"].Value = akt.fTLF_TIPUS;

                        try
                        {
                            cmd.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {
                            string s = "Hiba a partner telefonszámának mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                    else
                    {
                        cmd_insert.Parameters["TLF_SZAM"].Value = akt.fTLF_SZAM;
                        cmd_insert.Parameters["TLF_MEGJEGYZES"].Value = akt.fTLF_MEGJEGYZES;
                        cmd_insert.Parameters["TLF_PRT_ID"].Value = akt.fTLF_PRT_ID;
                        cmd_insert.Parameters["TLF_TIPUS"].Value = akt.fTLF_TIPUS;

                        try
                        {
                            cmd_insert.ExecuteNonQuery();
                            
                        }
                        catch (Exception e)
                        {
                            string s = "Hiba a partner telefonszámának mentése közben!";
                            Utilities.LogError(pBLObj, e, "clPARTNER", s);
                            oErr = s;
                            return false;
                        }
                    }
                }
                
            }


            return true;
        }

        public void FillPRT_ID(int iPRT_ID)
        {
            foreach (clPARTNER_TELEFON akt in lPARNTER_TELEFON)
            {
                akt.fTLF_PRT_ID = iPRT_ID;
            }
        }

    }

    public class clPARTNER
    {
        private SqlConnection sc;
        TBLObj pBLObj;

        public int fPRT_ID;
        public int fPRT_TIPUS;
        public string fPRT_NEV1;
        public string fPRT_NEV2;
        public DateTime fPRT_SZUL_DATUM;
        public string fPRT_IGSZAM;
        public string fPRT_MEGJEGYZES;
        public DateTime fPRT_CREATED;
        public int fPRT_CREATED_BY;
        public DateTime fPRT_MODIFIED;
        public int fPRT_MODIFIED_BY;

        public clPARTNER_MAIL_LIST lPARTNER_EMAIL;
        public clPARTNER_TELEFON_LIST lPARTNER_TELEFON;
        public clPARTNER_CIM_LIST lPARTNER_CIM;

        public string fPRT_NEM;


        public clPARTNER(TBLObj iBLObj)
        {
            pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;

            lPARTNER_EMAIL = new clPARTNER_MAIL_LIST(pBLObj);
            lPARTNER_TELEFON = new clPARTNER_TELEFON_LIST(pBLObj);
            lPARTNER_CIM = new clPARTNER_CIM_LIST(pBLObj);

        }
        public clPARTNER(TBLObj iBLObj , int iPRT_ID)
        {
            pBLObj = iBLObj;
            
            sc = iBLObj.sqlConnection;

            SqlCommand cmd = new SqlCommand();           

            cmd.Connection = sc;
            cmd.CommandText = "SELECT PRT_ID, PRT_TIPUS, PRT_NEV1, PRT_NEV2, PRT_SZUL_DATUM, PRT_IGSZAM, " +
                "PRT_MEGJEGYZES, PRT_CREATED, PRT_CREATED_BY, PRT_MODIFIED, PRT_MODIFIED_BY , PRT_NEM " +
                "FROM PARTNER " +
                "WHERE PRT_ID = " + iPRT_ID.ToString() + " ";

            cmd.CommandType = CommandType.Text;

            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                fPRT_ID = iPRT_ID ;
                fPRT_TIPUS = (int)rdr["PRT_TIPUS"];
                fPRT_NEV1 = rdr["PRT_NEV1"].ToString();
                fPRT_NEV2 = rdr["PRT_NEV2"].ToString();
                fPRT_SZUL_DATUM = (DateTime)rdr["PRT_SZUL_DATUM"];
                fPRT_IGSZAM = rdr["PRT_IGSZAM"].ToString();
                fPRT_MEGJEGYZES = rdr["PRT_IGSZAM"].ToString();
                fPRT_CREATED = Convert.ToDateTime(rdr["PRT_CREATED"]);
                fPRT_CREATED_BY =  (int)rdr["PRT_CREATED_BY"] ;
                fPRT_MODIFIED = Convert.ToDateTime(rdr["PRT_MODIFIED"]);
                fPRT_MODIFIED_BY = (int)rdr["PRT_MODIFIED_BY"];
                fPRT_NEM = rdr["PRT_NEM"].ToString();
            }
            rdr.Close();

            lPARTNER_EMAIL = new clPARTNER_MAIL_LIST(pBLObj, fPRT_ID);

            lPARTNER_TELEFON = new clPARTNER_TELEFON_LIST(pBLObj,fPRT_ID);

            lPARTNER_CIM = new clPARTNER_CIM_LIST(pBLObj, fPRT_ID);

        }        

        public bool SavePartner(ref string oErr)
        {
            bool ret = true ;
            SqlCommand cmd = new SqlCommand();
            SqlCommand cmd_insert = new SqlCommand();

            cmd.Connection = sc;
            cmd.CommandType = CommandType.Text;

            cmd_insert.Connection = sc;
            cmd_insert.CommandType = CommandType.Text;


            cmd.CommandText = "UPDATE PARTNER SET " +
                " PRT_TIPUS = @PRT_TIPUS ," +
                " PRT_NEV1 = @PRT_NEV1 ," +
                " PRT_NEV2 = @PRT_NEV2 ," +
                " PRT_SZUL_DATUM = @PRT_SZUL_DATUM, " +
                " PRT_IGSZAM = @PRT_IGSZAM, " +
                " PRT_MEGJEGYZES = @PRT_MEGJEGYZES, " +
                " PRT_MODIFIED_BY = @PRT_MODIFIED_BY, " +
                " PRT_MODIFIED = @PRT_MODIFIED ," +
                " PRT_NEM = @PRT_NEM " +
                "WHERE PRT_ID = @PRT_ID ";

            cmd.Parameters.Add(new SqlParameter("PRT_ID", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("PRT_NEV1", SqlDbType.VarChar ));
            cmd.Parameters.Add(new SqlParameter("PRT_NEV2", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("PRT_SZUL_DATUM", SqlDbType.DateTime ));
            cmd.Parameters.Add(new SqlParameter("PRT_IGSZAM", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("PRT_TIPUS", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("PRT_MEGJEGYZES", SqlDbType.VarChar));
            cmd.Parameters.Add(new SqlParameter("PRT_MODIFIED_BY", SqlDbType.Int));
            cmd.Parameters.Add(new SqlParameter("PRT_MODIFIED", SqlDbType.DateTime));
            cmd.Parameters.Add(new SqlParameter("PRT_NEM", SqlDbType.VarChar));

            cmd_insert.CommandText = "INSERT INTO PARTNER " +
                "( PRT_ID, " +
                " PRT_TIPUS, PRT_NEV1, PRT_NEV2, PRT_SZUL_DATUM, PRT_IGSZAM, " +
                " PRT_MEGJEGYZES, PRT_CREATED, PRT_CREATED_BY, PRT_MODIFIED, PRT_MODIFIED_BY, PRT_NEM) " +
                "VALUES " +
                "(@PRT_ID, " +
                " @PRT_TIPUS, @PRT_NEV1, @PRT_NEV2, @PRT_SZUL_DATUM, @PRT_IGSZAM, " +
                " @PRT_MEGJEGYZES, @PRT_CREATED, @PRT_CREATED_BY, @PRT_MODIFIED, @PRT_MODIFIED_BY, @PRT_NEM)";

            cmd_insert.Parameters.Add(new SqlParameter("PRT_ID", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_NEV1", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_NEV2", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_SZUL_DATUM", SqlDbType.DateTime));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_IGSZAM", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_TIPUS", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_MEGJEGYZES", SqlDbType.VarChar));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_CREATED_BY", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_CREATED", SqlDbType.DateTime));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_MODIFIED_BY", SqlDbType.Int));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_MODIFIED", SqlDbType.DateTime));
            cmd_insert.Parameters.Add(new SqlParameter("PRT_NEM", SqlDbType.VarChar));


            if (fPRT_ID != -1)
            {
                cmd.Parameters["PRT_ID"].Value = fPRT_ID;
                cmd.Parameters["PRT_NEV1"].Value = fPRT_NEV1;
                cmd.Parameters["PRT_NEV2"].Value = fPRT_NEV2;
                cmd.Parameters["PRT_SZUL_DATUM"].Value = fPRT_SZUL_DATUM;
                cmd.Parameters["PRT_IGSZAM"].Value = fPRT_IGSZAM;
                cmd.Parameters["PRT_TIPUS"].Value = fPRT_TIPUS;
                cmd.Parameters["PRT_MEGJEGYZES"].Value = fPRT_MEGJEGYZES;
                cmd.Parameters["PRT_MODIFIED_BY"].Value = fPRT_MODIFIED_BY;
                cmd.Parameters["PRT_MODIFIED"].Value = fPRT_MODIFIED;
                cmd.Parameters["PRT_NEM"].Value = fPRT_NEM;
                
                try
                {
                    cmd.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    string s = "Hiba a partner mentése közben!";
                    Utilities.LogError(pBLObj, e, "clPARTNER", s);
                    oErr = s;
                    return false;
                }
            }
            else
            {
                fPRT_ID = Bussines.Utilities.GenId(sc);
                cmd_insert.Parameters["PRT_ID"].Value = fPRT_ID;
                cmd_insert.Parameters["PRT_NEV1"].Value = fPRT_NEV1;
                cmd_insert.Parameters["PRT_NEV2"].Value = fPRT_NEV2;
                cmd_insert.Parameters["PRT_SZUL_DATUM"].Value = fPRT_SZUL_DATUM;
                cmd_insert.Parameters["PRT_IGSZAM"].Value = fPRT_IGSZAM;
                cmd_insert.Parameters["PRT_TIPUS"].Value = fPRT_TIPUS;
                cmd_insert.Parameters["PRT_MEGJEGYZES"].Value = fPRT_MEGJEGYZES;
                cmd_insert.Parameters["PRT_MODIFIED_BY"].Value = fPRT_MODIFIED_BY;
                cmd_insert.Parameters["PRT_MODIFIED"].Value = fPRT_MODIFIED;
                cmd_insert.Parameters["PRT_CREATED_BY"].Value = fPRT_MODIFIED_BY;
                cmd_insert.Parameters["PRT_CREATED"].Value = fPRT_MODIFIED;
                cmd_insert.Parameters["PRT_NEM"].Value = fPRT_NEM;
                try
                {
                    cmd_insert.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    string s = "Hiba a partner mentése közben!";
                    Utilities.LogError(pBLObj, e, "clPARTNER", s);
                    oErr = s;
                    return false;
                }
            }

            lPARTNER_EMAIL.FillPRT_ID(fPRT_ID);
            ret = lPARTNER_EMAIL.SavePartnerEmail(ref oErr);

            lPARTNER_TELEFON.FillPRT_ID(fPRT_ID);
            ret = lPARTNER_TELEFON.SavePartnerTelefon(ref oErr);

            lPARTNER_CIM.FillPRT_ID(fPRT_ID);
            ret = lPARTNER_CIM.SavePartnerCim(ref oErr);

            return true;
            

        }

        //megnezeskor ez lefut, figyelmezteteskhez kell
        public bool UpdateModified(ref string oErr)
        {
            SqlCommand cmd = new SqlCommand();

            oErr = "";
            
            cmd.Connection = sc;
            cmd.CommandText = "UPDATE PARTNER SET " +
                " PRT_MODIFIED_BY = PRT_MODIFIED_BY " +
                "WHERE PRT_ID = " + pBLObj.USR_ID.ToString();

            cmd.CommandType = CommandType.Text;
            try
            {
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e)
            {
                string s = "Hiba a partner megnézésének mentése közben!";
                Utilities.LogError(pBLObj, e, "clPARTNER", s);
                oErr = s;
                return false;
            }
        }
  
    }


    public class clPARTNER_Choice
    {
        int _USR_ID;
        private SqlConnection sc;

        SqlDataAdapter adapterPARTNER;

        DataSet dsPARTNER;

        public clPARTNER_Choice(TBLObj iBLObj, int iUSR_ID)
        {
            TBLObj pBLObj = iBLObj;
            sc = iBLObj.sqlConnection;
            _USR_ID = iUSR_ID;
        }

        public DataSet GetPARTNER(string iWhereSQL , bool iAlaRendIs)
        {
            string SQL = "";
            dsPARTNER = new DataSet();
            adapterPARTNER = new SqlDataAdapter();

            SQL = "SELECT " +
                " PRT_ID, PRT_TIPUS, PRT_NEV1, PRT_NEV2, PRT_SZUL_DATUM, PRT_IGSZAM, PRT_MEGJEGYZES, PRT_CREATED, PRT_CREATED_BY, PRT_MODIFIED, PRT_MODIFIED_BY, PRT_NEM, " +
                " CMB_ID, CMB_PRT_ID, CMB_ORS_ID, CMB_IRS_ID, CMB_VAROS, CMB_UTCA, CMB_MEGJEGYZES, CMB_TIPUS, " +
                " TLF_ID, TLF_TIPUS, TLF_PRT_ID, TLF_SZAM, TLF_MEGJEGYZES, " +
                " EML_ID, EML_PRT_ID, EML_MAILCIM, EML_MEGJEGYZES, " +
                " TPS_MEGNEVEZES, " +
                " PRT_NEV1 + ' ' + PRT_NEV2 AS PRT_NEV, " +
                " IRS_SZAM + ' ' + CMB_VAROS + ' ' + CMB_UTCA AS CMB_CIM " +
                "FROM PARTNER P " +
                "INNER JOIN TIPUS ON TPS_KATEGORIA = 'PRT' AND TPS_TIPUSKOD = PRT_TIPUS " +
                "LEFT JOIN (SELECT TOP 1 * FROM PARTNER_CIM ORDER BY CMB_ID) AS PCIM ON CMB_PRT_ID = PRT_ID " +
                "LEFT JOIN (SELECT TOP 1 * FROM PARTNER_TELEFON ORDER BY TLF_ID) AS PTELEFON ON TLF_PRT_ID = PRT_ID " +
                "LEFT JOIN (SELECT TOP 1 * FROM PARTNER_MAIL ORDER BY EML_ID) AS PMAIL ON EML_PRT_ID = PRT_ID  " +
                "LEFT JOIN IRSZ ON IRS_ID = CMB_IRS_ID " ;
            if (iAlaRendIs)
            {
                SQL += "INNER JOIN dbo.FN_GET_PARTNERS(" + _USR_ID.ToString() + ") ON PRT_ID = FN_PRT_ID ";
            }
            else
            {
                SQL += "WHERE PRT_CREATED_BY = " + _USR_ID.ToString() + " " + iWhereSQL ; 
            }

            adapterPARTNER.SelectCommand = new SqlCommand(SQL , sc);
            adapterPARTNER.Fill(dsPARTNER, "PARTNER");

            return dsPARTNER;
        }
    }


}
